package chapter6.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import chapter6.entity.Customer;
@Controller
public class CustomerController{
    //绑定默认参数类型
    @RequestMapping(value="/add.do")
    protected ModelAndView userAdd(HttpServletRequest req){
        String name=req.getParameter("name");
        int age=Integer.parseInt(req.getParameter("age"));
        String[] hobby=req.getParameterValues("hobby");

        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName(name);
        c1.setAge(age);
        c1.setHobby(hobby);

        System.out.println("======"+c1);

        ModelAndView modelAndView = new ModelAndView("hello");
        return modelAndView;
    }
    @RequestMapping(value="/chapter_6_1")
    protected ModelAndView chapter_6_1(HttpServletRequest req){
        ModelAndView modelAndView = new ModelAndView("chapter_6_1_index");
        return modelAndView;
    }
    @RequestMapping(value="/chapter_6_2")
    protected ModelAndView chapter_6_2(HttpServletRequest req){
        ModelAndView modelAndView = new ModelAndView("chapter_6_2_index");
        return modelAndView;
    }
    @RequestMapping(value="/chapter_6_3")
    protected ModelAndView chapter_6_3(HttpServletRequest req){
        ModelAndView modelAndView = new ModelAndView("chapter_6_3_index");
        return modelAndView;
    }
    @RequestMapping(value="/chapter_6_4")
    protected ModelAndView chapter_6_4(HttpServletRequest req){
        ModelAndView modelAndView = new ModelAndView("chapter_6_4_index");
        return modelAndView;
    }


    //绑定简单数据类型
    @RequestMapping(value="/simple.do")
    protected ModelAndView simple(String name,int age,String[] hobby){

        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName(name);
        c1.setAge(age);
        c1.setHobby(hobby);

        System.out.println("======"+c1);

        ModelAndView modelAndView = new ModelAndView("hello");
        return modelAndView;
    }

    //绑定简单数据类型,@RequestParam注解使用
    @RequestMapping(value="/requestParam.do")
    protected ModelAndView requestParam(@RequestParam(value="name") String username,@RequestParam(value="age") int userage,String[] hobby){
        Customer c1 = new Customer();
        c1.setCustId(1);
        c1.setName(username);
        c1.setAge(userage);
        c1.setHobby(hobby);
        System.out.println("======"+c1);
        ModelAndView modelAndView = new ModelAndView("hello");
        return modelAndView;
    }
    //绑定POJO类型
    @RequestMapping(value="/pojo.do")
    protected ModelAndView pojo(Customer c1){
        System.out.println("======"+c1);
        ModelAndView modelAndView = new ModelAndView("hello");
        return modelAndView;
    }

}

